package com.yonyou.cyxdms.sysAuth.service.service.org;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.DMSAuthException;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.basedata.client.domains.vo.BrandCacheVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.SeriesCacheVO;
import com.yonyou.cyxdms.basedata.client.service.basedata.BaseCommonBrandService;
import com.yonyou.cyxdms.basedata.client.service.basedata.BaseCommonSeriesService;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.SysAuthCommonConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.SysAuthDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.OrgDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.area.OrgOutDto;
import com.yonyou.cyxdms.sysAuth.service.entity.ComOrganizationPO;
import com.yonyou.cyxdms.sysAuth.service.entity.OrgPO;
import com.yonyou.cyxdms.sysAuth.service.entity.TrDealerOrgRelationPO;
import com.yonyou.cyxdms.sysAuth.service.entity.TrOrgPO;
import com.yonyou.cyxdms.sysAuth.service.feign.channelclient.CmsIsChannelClient;
import com.yonyou.cyxdms.sysAuth.service.mapper.DealerInfoMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.OrgMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.TrDealerOrgRelationMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.TrOrgMapper;
import com.yonyou.cyxdms.sysAuth.service.vo.org.*;
import com.yonyou.cyxdms.sysAuth.service.vo.user.UserVO;
import com.yonyou.cyxdms.sysmanage.client.vo.org.OrgAndParentVO;
import com.yonyou.cyxdms.sysmanage.client.vo.org.OrgParentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 组织 服务实现类
 * </p>
 *
 * @author maxingjian
 * @since 2019-08-26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrgServiceImpl extends ServiceImpl<OrgMapper, OrgPO> implements OrgService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(OrgServiceImpl.class);
    @Autowired
    OrgMapper orgMapper;

    @Autowired
    TrOrgMapper trOrgMapper;

    @Autowired
    TrDealerOrgRelationMapper trDealerOrgRelationMapper;

    @Autowired
    BaseCommonBrandService baseCommonBrandService;

    @Autowired
    BaseCommonSeriesService baseCommonSeriesService;

    @Autowired
    DealerInfoMapper dealerInfoMapper;
    @Autowired
    CmsIsChannelClient cmsIsChannelClient ;
    /**
     * @author ：maxingjian
     * @date ：2019/8/26 15:34
     * @description： 通过id获取下级的组织
     */
    @Override
    public List<OrgVO> getChildOrg(String type, String orgId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return orgMapper.getChildOrg(loginInfoDto.getCompanyId(), orgId, type);
    }

    @Override
    public List<OrgVO> getChildOrgs() {
        return orgMapper.getChildOrgs();
    }

    @Override
    public List<OrgVO> getChildOrgs(String orgId) {
        return orgMapper.getChildOrgsc(orgId);
    }

    @Override
    public OrgDetailVO getOrgDetail(String orgId) {
        logger.debug("通过Id查询明细-orgId:{}", orgId);
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        OrgDetailVO orgDetailVO = orgMapper.getOrgDetail(orgId);
        List<OrgBrandSeriesVO> list = orgMapper.getOrgBrandSeries(orgId);
        BrandCacheVO brandCacheVO = null;
        SeriesCacheVO seriesCacheVO = null;
        if (list.size() > 0) {
            for (int i = list.size() - 1; i > -1; i--) {
                if (StringUtils.isEquals(list.get(i).getBillType(),
                        String.valueOf(SysAuthDictCodeConstants.BILL_TYPE_ORG_RELATION_BRAND))) {
                    brandCacheVO = baseCommonBrandService.getValueByBrandId(list.get(i).getBrandId().toString());
                    if (brandCacheVO != null) {
                        list.get(i).setBrandName(brandCacheVO.getBrandName());
                    } else {
                        list.remove(i);
                    }
                } else {
                    seriesCacheVO = baseCommonSeriesService.getValueBySeriesId(list.get(i).getSeriesId().toString());
                    if (seriesCacheVO != null) {
                        list.get(i).setSeriesName(seriesCacheVO.getSeriesName());
                        list.get(i).setBrandId(seriesCacheVO.getBrandId());
                        list.get(i).setBrandName(seriesCacheVO.getBrandName());
                    } else {
                        list.remove(i);
                    }
                }
            }
            orgDetailVO.setListobsvo(list);
        }
        return orgDetailVO;
    }

    @Override
    public List<BrandListVO> getOrgBrand(String orgId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        BrandCacheVO brandCacheVO = null;
        SeriesCacheVO seriesCacheVO = null;
        List<BrandListVO> brandlistvolist = new ArrayList<BrandListVO>();
        if (StringUtils.isNullOrEmpty(orgId) || "0".equals(orgId)) {
            List<BrandCacheVO> list = baseCommonBrandService.listAllBrand();
            for (int i = 0; i < list.size(); i++) {
                BrandListVO brandListVO = new BrandListVO();
                brandListVO.setBrandId(list.get(i).getBrandId());
                brandListVO.setBrandCode(list.get(i).getBrandCode());
                brandListVO.setBrandName(list.get(i).getBrandName());
                brandlistvolist.add(brandListVO);
            }
            return brandlistvolist;
        }
        List<OrgBrandVO> list = orgMapper.getOrgBrand(orgId, null);
        for (int i = 0; i < list.size(); i++) {
            if (StringUtils.isEquals(list.get(i).getBillType(),
                    String.valueOf(SysAuthDictCodeConstants.BILL_TYPE_ORG_RELATION_BRAND))) {
                brandCacheVO = baseCommonBrandService.getValueByBrandId(list.get(i).getBillId().toString());
                BrandListVO brandListVO = new BrandListVO();
                brandListVO.setBrandId(brandCacheVO.getBrandId());
                brandListVO.setBrandCode(brandCacheVO.getBrandCode());
                brandListVO.setBrandName(brandCacheVO.getBrandName());
                brandlistvolist.add(brandListVO);
            } else {
                seriesCacheVO = baseCommonSeriesService.getValueBySeriesId(list.get(i).getBillId().toString());
                if (brandlistvolist.size() == 0) {
                    brandlistvolist.add(setVO(seriesCacheVO));
                } else {
                    int count = brandlistvolist.size();
                    for (int j = 0; j < count; j++) {
                        if (!brandlistvolist.get(j).getBrandId().toString().equals(seriesCacheVO.getBrandId().toString())) {
                            brandlistvolist.add(setVO(seriesCacheVO));
                        }
                    }
                }
            }
        }
        return brandlistvolist;
    }

    private BrandListVO setVO(SeriesCacheVO seriesCacheVO) {
        BrandListVO brandListVO = new BrandListVO();
        brandListVO.setBrandId(seriesCacheVO.getBrandId());
        brandListVO.setBrandCode(seriesCacheVO.getBrandCode());
        brandListVO.setBrandName(seriesCacheVO.getBrandName());
        return brandListVO;
    }

    @Override
    public List<SeriesListVO> getOrgSeries(String orgId, String brandId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        SeriesCacheVO seriesCacheVO = null;
        List<SeriesListVO> seriesListVOList = new ArrayList<SeriesListVO>();
        if (brandId == null || brandId == "") {
            return null;
        }
        if (StringUtils.isNullOrEmpty(orgId) || "0".equals(orgId)) {
            List<SeriesCacheVO> seriescachevolist = baseCommonSeriesService.getSeriesByBrandId(brandId);
            for (int i = 0; i < seriescachevolist.size(); i++) {
                SeriesListVO seriesListVO = new SeriesListVO();
                seriesListVO.setBrandId(seriescachevolist.get(i).getBrandId());
                seriesListVO.setSeriesCode(seriescachevolist.get(i).getSeriesCode());
                seriesListVO.setSeriesName(seriescachevolist.get(i).getSeriesName());
                seriesListVO.setSeriesId(seriescachevolist.get(i).getSeriesId());
                seriesListVOList.add(seriesListVO);
            }
        } else {
            List<OrgBrandVO> orgbrandvolist = orgMapper.getOrgBrand(orgId,
                    String.valueOf(SysAuthDictCodeConstants.BILL_TYPE_ORG_RELATION_SERIES));
            for (int i = 0; i < orgbrandvolist.size(); i++) {
                seriesCacheVO = baseCommonSeriesService.getValueBySeriesId(orgbrandvolist.get(i).getBillId().toString());
                SeriesListVO seriesListVO = new SeriesListVO();
                seriesListVO.setBrandId(seriesCacheVO.getBrandId());
                seriesListVO.setSeriesId(seriesCacheVO.getSeriesId());
                seriesListVO.setSeriesCode(seriesCacheVO.getSeriesCode());
                seriesListVO.setSeriesName(seriesCacheVO.getSeriesName());
                seriesListVOList.add(seriesListVO);
            }
        }
        return seriesListVOList;
    }

    @Override
    @Transactional
    public OrgDetailVO saveDetailOrg(OrgDetailVO orgDetailVO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        logger.debug(new Gson().toJson(loginInfoDto));
        if (StringUtils.isNullOrEmpty(orgDetailVO.getOrgId())) {
            //保存组织
            OrgPO orgpo = new OrgPO();
            String orgCode = orgDetailVO.getOrgCode();
            List<Long> orgIdByCode = orgMapper.findOrgIdByCode(orgCode, loginInfoDto.getCompanyId().intValue());
            if (orgIdByCode.size() > 0) {
                throw new DMSAuthException(SysAuthCommonConstants.ERROR_CODE_996, "code重复，不能新增");
            }
            fileddata(orgpo, loginInfoDto, orgDetailVO);
            orgpo.setOrgId(Long.MAX_VALUE);
            orgMapper.insert(orgpo);
            orgDetailVO.setOrgId(orgpo.getOrgId().toString());
            //保存关系表
            TrOrgPO trorgpo = new TrOrgPO();
//            trOrgPO.setAppId(loginInfoDto.getAppId());
            trorgpo.setOrgId(orgpo.getOrgId());
            trorgpo.setParentOrgId(Long.parseLong(orgDetailVO.getParentOrgId()));
            trOrgMapper.insert(trorgpo);

            //保存品牌
            insertDealerOrgRelation(orgDetailVO, orgpo.getOrgId().intValue());
        } else {
            //修改组织
            OrgPO orgpo = orgMapper.selectById(orgDetailVO.getOrgId());
            String orgCode = orgDetailVO.getOrgCode();
            List<Long> orgIdByCode = orgMapper.findOrgIdByCode(orgCode, loginInfoDto.getCompanyId().intValue());
            if (orgIdByCode.size() > 0) {
                if ((orgIdByCode.size() == 1 && !orgIdByCode.get(0).equals(orgpo.getOrgId())) || orgIdByCode.size() > 1) {
                    throw new DMSAuthException(SysAuthCommonConstants.ERROR_CODE_996, "code重复，不能新增");
                }
            }

            if (new Integer(SysAuthDictCodeConstants.STATUS_NOT_VALID).equals(orgDetailVO.getIsValid())) {
                // 查询当前公司是否有下级
                List<TrOrgPO> trorgpos = trOrgMapper.selectList(new LambdaQueryWrapper<TrOrgPO>()
                        .eq(TrOrgPO::getParentOrgId, orgDetailVO.getOrgId()));
                if(!CollectionUtils.isEmpty(trorgpos)){
                    List<Long> orgIds = trorgpos.stream().map(po -> po.getOrgId()).collect(Collectors.toList());
                    List<OrgPO> orgpos = orgMapper.selectList(new LambdaQueryWrapper<OrgPO>()
                            .eq(OrgPO::getIsValid, SysAuthDictCodeConstants.STATUS_IS_VALID)
                            .in(OrgPO::getOrgId, orgIds));
                    if(!CollectionUtils.isEmpty(orgpos)){
                        throw new DMSAuthException(SysAuthCommonConstants.ERROR_CODE_996, "当前部门存在下级，不允许设为无效！");
                    }
                }
                //状态无效删除区域信息
                cmsIsChannelClient.deleteAreaById(orgpo.getOrgId());
            }

            fileddata(orgpo, loginInfoDto, orgDetailVO);
            orgMapper.updateById(orgpo);
            //修改小区的区域信息
            if (orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SALE_WAR)||orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SALE_BIG)||orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SALE_SMALL) || orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SERVICE_WAR) || orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SERVICE_BIG)||orgDetailVO.getOrgType().equals(ManageDictCodeConstants.ORG_TYPE_SERVICE_SMALL)){
                cmsIsChannelClient.updateAreaById(orgpo.getOrgId(),orgDetailVO.getOrgName());
            }
            orgDetailVO.setRecordVersion(orgpo.getRecordVersion());

            //删除品牌
            trDealerOrgRelationMapper.deleteById(orgDetailVO.getOrgId());
            //保存品牌
            insertDealerOrgRelation(orgDetailVO, orgpo.getOrgId().intValue());
        }
        return orgDetailVO;
    }

    @Override
    public void insertDealerOrgRelation(OrgDetailVO orgDetailVO, Integer id) {
        QueryWrapper<TrDealerOrgRelationPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", id);
        trDealerOrgRelationMapper.delete(queryWrapper);
        int type;
        if (CollectionUtils.isEmpty(orgDetailVO.getListobsvo())) {
            return;
        }
        for (int i = 0; i < orgDetailVO.getListobsvo().size(); i++) {
            TrDealerOrgRelationPO trdealerorgrelationpo = new TrDealerOrgRelationPO();
            trdealerorgrelationpo.setOrgId(id);
            type = orgDetailVO.getListobsvo().get(i).getBillType().intValue();
            trdealerorgrelationpo.setBillType(type);
            if (type == SysAuthDictCodeConstants.BILL_TYPE_ORG_RELATION_BRAND) {
                Long brandId = orgDetailVO.getListobsvo().get(i).getBrandId();
                if (brandId != null) {
                    trdealerorgrelationpo.setBillId(brandId.intValue());
                    trDealerOrgRelationMapper.insert(trdealerorgrelationpo);
                    orgDetailVO.getListobsvo().get(i).setOrgId(String.valueOf(id));
                }

            } else {
                Long seriesId = orgDetailVO.getListobsvo().get(i).getSeriesId();
                if (seriesId != null) {
                    trdealerorgrelationpo.setBillId(seriesId.intValue());
                    trDealerOrgRelationMapper.insert(trdealerorgrelationpo);
                    orgDetailVO.getListobsvo().get(i).setOrgId(String.valueOf(id));
                }
            }
        }
    }

    private void fileddata(OrgPO orgpo, LoginInfoDto loginInfoDto, OrgDetailVO orgDetailVO) {
        orgpo.setCompanyId(loginInfoDto.getCompanyId());
        orgpo.setAppId(loginInfoDto.getAppId());
        orgpo.setOrgCode(orgDetailVO.getOrgCode());
        orgpo.setOrgName(orgDetailVO.getOrgName());
        orgpo.setOrgDesc(orgDetailVO.getOrgDesc());
        orgpo.setOrgType(orgDetailVO.getOrgType());
        orgpo.setIsValid(orgDetailVO.getIsValid());
        orgpo.setDataType(orgDetailVO.getDataType());
        orgpo.setRecordVersion(orgDetailVO.getRecordVersion());
    }

    @Override
    public OrgAndParentVO getOrg(String orgId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        if (StringUtils.isNullOrEmpty(orgId)) {
            orgId = loginInfoDto.getOrgId().toString();
        }
        //获取当前组织的父级ordId
        List<Long> parentOrgIds = orgMapper.getParentOrgId(Long.parseLong(orgId));

        String orgIds = " ";
        for (int i = 0; i < parentOrgIds.size() - 1; i++) {
            orgIds += parentOrgIds.get(i) + " , ";
        }
        orgIds += parentOrgIds.get(parentOrgIds.size() - 1);

        String[] split = orgIds.split(",");
        //获取组织信息
        List<OrgParentVO> orgvos = orgMapper.getOrgByOrgIds(split);

        OrgAndParentVO orgAndParentVO = orgMapper.getOrgByOrgId(Long.parseLong(orgId));
        orgAndParentVO.setOrgParentVOS(orgvos);


        return orgAndParentVO;
    }


    /**
     * 新增组织
     *
     * @auther: 李贺
     * @since: 2019/9/10
     */
    @Override
    public void addOrg(OrgPO orgpo) {
        orgMapper.insert(orgpo);
    }

    /**
     * 新增组织关系
     *
     * @auther: 李贺
     * @since: 2019/9/10
     */
    @Override
    public void addTrOrg(TrOrgPO trorgpo) {
        trOrgMapper.insert(trorgpo);
    }

    /**
     * 根据appId获取组织列表
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/9/19
     */
    @Override
    public List<OrgDTO> getOrgList() {
        List<OrgDTO> result = new ArrayList<>();
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        // String orgType = loginInfoDto.getOrgType().toString();
        String companyId = loginInfoDto.getCompanyId().toString();
        List<OrgByTypeVO> orgByOrgType = this.getOrgByOrgType(String.valueOf(ManageDictCodeConstants.ORG_TYPE_SALEAREA), companyId, loginInfoDto.getAppId());

        for (OrgByTypeVO dealerOrgAreaVO : orgByOrgType) {
            OrgDTO orgDTO = new OrgDTO();
            orgDTO.setOrgId(dealerOrgAreaVO.getOrgId());
            orgDTO.setOrgName(dealerOrgAreaVO.getOrgName());
            orgDTO.setOrgShortName(dealerOrgAreaVO.getOrgName());
            result.add(orgDTO);
        }
     /*
        LoginInfoDto login = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String appId = login.getAppId();
        String ownerCode = login.getOwnerCode();
        List<DealerOrgAreaVO> dealerBySaleArea = dealerInfoMapper.getDealerBySaleArea(null, login.getCompanyId().toString(), appId, null, null);

        for (DealerOrgAreaVO dealerOrgAreaVO : dealerBySaleArea) {
            OrgDTO orgDTO= new OrgDTO();
            orgDTO.setOrgId(dealerOrgAreaVO.getOrgId());
            orgDTO.setOrgName(dealerOrgAreaVO.getOrgName());
            orgDTO.setOrgShortName(dealerOrgAreaVO.getOrgName());
            result.add(orgDTO);
        }*/
        return result;
    }

    /**
     * 根据类型查询相应一组id
     *
     * @param:
     * @return:
     * @auther: MAXINGJIAN
     * @since: 2019/9/26
     */
    @Override
    public List<OrgByTypeVO> getOrgByOrgType(String orgType, String companyId, String appId) {
        LoginInfoDto login = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        if (StringUtils.isNullOrEmpty(companyId)) {
            companyId = login.getCompanyId().toString();
        }
        if (StringUtils.isNullOrEmpty(appId)) {
            appId = login.getAppId();
        }
        String[] orgTypes = orgType.split(",");
        return orgMapper.getOrgByOrgType(orgTypes, companyId, appId);
    }

    /**
     * 根据类型查询相应一组id
     *
     * @param:
     * @return:
     * @auther: MAXINGJIAN
     * @since: 2019/9/26
     */
    @Override
//    public List<OrgByTypeVO> getOrgByOrgType(String orgType, String companyId, String appId) {
    public List<OrgByTypeVO> getOrgByOrgType4cms(String orgType, String companyId, String appId ,String parentOrgId,
                                             String orgCode, String orgName) {

        LoginInfoDto login = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
//        if (StringUtils.isNullOrEmpty(companyId)) {
//            companyId = login.getCompanyId().toString();
//        }
        if (StringUtils.isNullOrEmpty(appId)) {
            appId = login.getAppId().toString();
        }
//        return orgMapper.getOrgByOrgType(orgType, companyId, appId);
        return orgMapper.getOrgByOrgType4cms(orgType, companyId, appId , parentOrgId,orgCode,orgName);
    }

    @Override
    public GmsOrgVO getGmsOrg(Long orgId) {
        return orgMapper.findOrgInfo(orgId);
    }

    /**
     * DEALERCODE获取ORG信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/30
     */
    @Override
    public Map<Long, OrgDealerVO> getOrgNameByOwnerCode(String appId, Map<Long, String> ownercodes) {
        Map<String, OrgDealerVO> cacheMap = new HashMap<>();  //缓存MAP
        Map<Long, OrgDealerVO> result = new HashMap<>();
        Set<Map.Entry<Long, String>> entries = ownercodes.entrySet();
        for (Map.Entry<Long, String> entry : entries) {
            String dealercode = entry.getValue();
            //先判断缓存是否存在
            OrgDealerVO tempOrg = cacheMap.get(appId + dealercode);
            if (tempOrg != null) {
                result.put(entry.getKey(), tempOrg);
                continue;
            }
            Map<String, String> queryMap = this.orgMapper.getOrgNameByOwnerCode(appId, dealercode);
            OrgDealerVO orgDealerVO = new OrgDealerVO();
            if (queryMap != null) {
                orgDealerVO.setOrgName(queryMap.get("ORG_NAME"));
                orgDealerVO.setDealerCode(queryMap.get("DEALER_CODE"));
                orgDealerVO.setDealerShortName(queryMap.get("DEALER_SHORTNAME"));
                result.put(entry.getKey(), orgDealerVO);
                //添加入缓存
                cacheMap.put(appId + dealercode, orgDealerVO);
            } else {
                result.put(entry.getKey(), null);
            }


        }
        return result;
    }

    @Override
    public void delDealerOrgRelation(Long orgRelationId) {
        trDealerOrgRelationMapper.deleteById(orgRelationId);
    }

    /**
     * 获取所有销售部门
     *
     * @return
     * @author 李贺
     * @since 2019/11/20
     */
    @Override
    public List<OrgParentVO> getSaleOrg(Long orgId) {
        String orgIds;
        if (orgId == null) {
            LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
            orgIds = loginInfoDto.getOrgIds();
        } else {
            orgIds = orgMapper.getOrgs(orgId);
        }
        String[] orgIdList = orgIds.split(",");
        List<Integer> orgTypeList = getSaleOrgType();
        return orgMapper.getOrgByOrgsAndOrgTypes(orgIdList, orgTypeList);
    }

    @Override
    public List<Integer> getSaleOrgType() {
        List<Integer> orgTypeList = new ArrayList<>();
        orgTypeList.add(15061013);
        return orgTypeList;
    }

    @Override
    public void deleteById(Long id, Long companyId) {
        orgMapper.deleteById(id, companyId);
    }


    /**
     * 获取所有事业部
     *
     * @return
     * @author 李贺
     * @since 2019/11/25
     */
    @Override
    public List<OrgParentVO> getBusinessOrg() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return orgMapper.getBusinessOrg(loginInfoDto.getAppId());
    }


    /**
     * 获取所有部门
     *
     * @return
     * @author 王圣童
     * @since 2021/11/25
     */
    @Override
    public List<OrgParentVO> getBusinessOrgAll() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return orgMapper.getBusinessOrgAll(loginInfoDto.getAppId());
    }
    /**
     * 通过orgID获得所有人
     *
     * @return
     * @author 李贺
     * @since 2019/11/25
     */
    @Override
    public List<UserVO> getUserByOrgId(Long orgId) {
        return orgMapper.getUserByOrgId(orgId);
    }

    @Override
    public List<OrgParentVO> getOrgByOrgIds(String orgId) {
        String[] orgIds = orgId.split(",");
        return orgMapper.getOrgByOrgIds(orgIds);
    }

    /**
     * 通过上级orgID和下属组织类型获得下属组织
     *
     * @return
     * @author 李贺
     * @since 2019/12/13
     */
    @Override
    public List<OrgVO> getOrgByTypeAndParent(Long parentOrgId, Long orgType) {
        return orgMapper.getOrgByTypeAndParent(parentOrgId,orgType);
    }

    /**
     * 获得销售小组
     *
     * @return
     * @author 李贺
     * @since 2020/1/7
     */
    @Override
    public List<OrgByTypeVO> listSalesTeam() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return orgMapper.listSalesTeam(loginInfoDto.getCompanyId());
    }

    @Override
    public List<OrgParentVO> getBusinessOrgAllByAppId(String appId) {
        return orgMapper.getBusinessOrgAll(appId);
    }

    @Override
    public List<OrgOutDto> getOrgAllOut() {
        return orgMapper.getOrgAllOut();
    }
}
